﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RSALicUI
{
    public partial class QRCodeParseProForm : FormBase
    {
        public QRCodeParseProForm()
        {
            InitializeComponent();
            this.qrParseCtrl.QrDetected += QrParseCtrl_KeyDetected;
        }

        private void QrParseCtrl_KeyDetected(string res)
        {
            if (string.IsNullOrEmpty(res))
            {
                res =
                    "识别失败！可尝试以下操作：" +
                    "    1.重新准确框选出二维码" +
                    "    2.更换图片" +
                    "    3.放大当前窗口，并重新框选二维码";
            }
            tbQrResult.Text = res;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            TryGetResult();
            this.qrParseCtrl.DrawRectFinished += QrParseCtrl_DrawRectFinished;
            this.qrParseCtrl.ParseImageGeted += QrParseCtrl_ParseImageGeted;
        }

        public void SetQrImage(Image qr)
        {
            this.qrParseCtrl.SetData(qr);
            TryGetResult();
        }

        private void QrParseCtrl_ParseImageGeted(Image obj)
        {
            this.panelPic.BackgroundImage = obj;
        }

        private void QrParseCtrl_DrawRectFinished()
        {
            TryGetResult();
        }

        void TryGetResult()
        {
            var res = GetQrResult();

            if (string.IsNullOrEmpty(res))
            {
                res = 
                    "识别失败！可尝试以下操作：\r\n" +
                    "    1.重新准确框选出二维码\r\n" +
                    "    2.更换图片\r\n" +
                    "    3.放大当前窗口，并重新框选二维码\r\n";
            }
            tbQrResult.Text = res;
        }

        public string GetQrResult()
        {
            return this.qrParseCtrl.GetQrData();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}
